home *** CD-ROM | disk | FTP | other *** search
/ CGI How-To / CGI HOW-TO.iso / chap2 / 2_4 / testgway.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-06-15  |  2.1 KB  |  89 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. /* function prototype */
  6. int getvalue(char *s, char** name, char** version);
  7.  
  8. main()
  9. {
  10.     char *gateway_interface;
  11.     char *name, *version;
  12.  
  13.     /* output html MIME type */
  14.     printf("Content-type: text/html\n\n");
  15.  
  16.     printf("<HTML>\n");
  17.     printf("<HEAD><TITLE>CGI Script How-to: Test Script</TITLE></HEAD>\n");
  18.     printf("<BODY>\n");
  19.  
  20.     printf("<H1>CGI Script How-To determine the version of CGI being used</H1>\n");
  21.  
  22.     gateway_interface = getenv("GATEWAY_INTERFACE");
  23.  
  24.     /* If name/version strings have been extracted then getvalue returns 0
  25.      * otherwise the value is NULL or in the wrong format.
  26.      */
  27.  
  28.     if (getvalue(gateway_interface, &name, &version) == 0)
  29.     {
  30.         /* Use the gateway interface value here */
  31.         printf("Gateway Interface: name = <B>%s</B> version = <B>%s</B>\n", name, version);
  32.     }
  33.     else
  34.     {
  35.         printf("Gateway interface is undefined or invalid!\n");
  36.     }
  37.  
  38.     printf("</BODY></HTML>\n\n");
  39.     exit(0);
  40. }
  41.  
  42.  
  43. /*
  44.  * function getvalue()
  45.  *
  46.  * Parses an input string (s) in the form name/version, extracts both the name
  47.  * and version elements and stores these in two output string variables (name,
  48.  * version).
  49.  *
  50.  *      Returns:  0 if name/version values extracted from target string,
  51.  *               -1 if values not present
  52.  */
  53.  
  54. int getvalue(char *s, char** name, char** version)
  55. {
  56.         char *p;
  57.         if (s == NULL || *s == '/')
  58.         {
  59.         return -1;              /* null string or no name field */
  60.         }
  61.  
  62.         p = strchr(s, '/');             /* Locate the slash (/) in the string */
  63.         if (p == 0)
  64.         {
  65.                 return -1;              /* '/' character not found */
  66.         }
  67.  
  68.         *name = malloc(p-s+1);
  69.         if (*name == NULL)
  70.         {
  71.                 return -1;                      /* malloc failed */
  72.         }
  73.         strncpy(*name, s, p-s);
  74.     (*name)[p-s] = '\0';              /* terminate the string */
  75.  
  76.         *version = malloc(strlen(p));
  77.         if (*version == NULL)
  78.         {
  79.                 return -1;                      /* malloc failed */
  80.         }
  81.         strcpy(*version, p+1);
  82.  
  83.         return 0;                               /* okay, value set */
  84. }
  85.  
  86. /*
  87.  * end of testgway.c
  88.  */
  89.